﻿/*
Copyright © 2011 François Karman

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

See <license.txt> for the complete license of the software.
*/

using System;
using System.Collections.Generic;

namespace Simple.Storage.Api
{
    /// <summary>
    /// Implements a simple cache used when loading object to avoid cycle.
    /// </summary>
    internal class LoadingCache
    {
        /// <summary>
        /// The loaded data.
        /// </summary>
        private readonly IDictionary<Type, IDictionary<object, object>> cache;

        /// <summary>
        /// Initializes a new instance of the LoadingCache class.
        /// </summary>
        public LoadingCache()
        {
            this.cache = new Dictionary<Type, IDictionary<object, object>>();
        }

        /// <summary>
        /// Checks if a specified data is already cached.
        /// </summary>
        /// <param name="type">The type of the object.</param>
        /// <param name="key">The key of the object.</param>
        /// <returns>
        /// <c>true</c> if an object of type <paramref name="type"/> with the specific <paramref name="key"/>
        /// is cached; <c>false</c> otherwise.
        /// </returns>
        public bool IsLoaded(Type type, object key)
        {
            return type != null
                && this.cache.ContainsKey(type)
                && this.cache[type].ContainsKey(key);
        }

        /// <summary>
        /// Retrieves a specified data from the cache.
        /// </summary>
        /// <param name="type">The type of the object.</param>
        /// <param name="key">The key of the object.</param>
        /// <returns>
        /// The object of type <paramref name="type"/> associated with <paramref name="key"/>.
        /// </returns>
        /// <exception cref="NotSupportedException">
        /// The cache doesn't contain the specified <paramref name="key"/> for the specified <paramref name="type"/>.
        /// </exception>
        public object Retrieve(Type type, object key)
        {
            if (!this.cache.ContainsKey(type) || !this.cache[type].ContainsKey(key))
            {
                // TODO: message
                throw new NotSupportedException();
            }

            return this.cache[type][key];
        }

        /// <summary>
        /// Registers an object into the cache.
        /// </summary>
        /// <param name="type">The type of the object.</param>
        /// <param name="key">The key of the object.</param>
        /// <param name="instance">The object.</param>
        public void Loaded(Type type, object key, object instance)
        {
            if (!this.cache.ContainsKey(type))
            {
                this.cache.Add(type, new Dictionary<object, object>());
            }

            if (!this.cache[type].ContainsKey(key))
            {
                this.cache[type].Add(key, instance);
            }
        }

        /// <summary>
        /// Clears the cache.
        /// </summary>
        public void Clear()
        {
            this.cache.Clear();
        }
    }
}
